﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//using LitJson;
using System.IO;
using System.Text;

public sealed class ConfigManager : Singleton<ConfigManager>
{
    private ConfigManager() { }

    private Dictionary<int, MetaData.MonsterData> mMonsterDataDic;
    private List<MetaData.ChainMonsterData> mChinaDataList;


    public void Init()
    {
        string monsterCSVS = string.Empty;
        string gameCSVS = string.Empty;
        string chainCSVS = string.Empty;


#if UNITY_EDITOR
        TextAsset monsterCSV = (TextAsset)Resources.Load("Config/Monster");
        TextAsset gameCSV = (TextAsset)Resources.Load("Config/Game");
        TextAsset chainCSV = (TextAsset)Resources.Load("Config/ChainMonster");


        Debug.Log("读取Resources配置文件");
        monsterCSVS = monsterCSV.text;
        gameCSVS = gameCSV.text;
        chainCSVS = chainCSV.text;


#else
        string path = System.Environment.CurrentDirectory + @"\config\";
        monsterCSVS = File.ReadAllText(path + "Monster.csv", Encoding.ASCII);
        gameCSVS = File.ReadAllText(path + "Game.csv", Encoding.ASCII);
        chainCSVS = File.ReadAllText(path + "ChainMonster.csv", Encoding.ASCII);

        Debug.Log("读取Config文件夹配置文件");
#endif

        int index = 0;
        string text = monsterCSVS;
        string[] lineAry = text.Split('\n');

        #region 分析怪物表

        mMonsterDataDic = new Dictionary<int, MetaData.MonsterData>();
        MetaData.MonsterData ms = null;
        for (int i = 0; i < lineAry.Length; i++)
        {
            if (lineAry[i].StartsWith("#"))
                continue;

            if (string.IsNullOrEmpty(lineAry[i]))
                continue;

            lineAry[i] = lineAry[i].Replace("\r", "");
            lineAry[i] = lineAry[i].TrimEnd(',');


            string[] lie = lineAry[i].Split(',');
            ms = new MetaData.MonsterData();

            ms.Id = Convert.ToInt32(lie[index++]);
            ms.Type = Convert.ToInt32(lie[index++]);
            ms.HP = Convert.ToInt32(lie[index++]);
            ms.Speed = Convert.ToSingle(lie[index++]);
            ms.Harm = Convert.ToInt32(lie[index++]);
            ms.Difficulty = Convert.ToInt32(lie[index++]);

            ms.CopperRatio = Convert.ToInt32(lie[index++]);
            ms.TicketRatio = Convert.ToInt32(lie[index++]);
            ms.RuneStoneRatio = Convert.ToInt32(lie[index++]);

            ms.CopperNum = Convert.ToInt32(lie[index++]);
            ms.TicketNum = Convert.ToInt32(lie[index++]);
            ms.RuneStoneNum = Convert.ToInt32(lie[index++]);
            ms.AssetPath = Convert.ToString(lie[index++]);
            ms.GoldRatio = Convert.ToInt32(lie[index++]);
            ms.ScreenEffect = Convert.ToString(lie[index++]);
            ms.AudioName = Convert.ToString(lie[index++]);
            ms.Energy = Convert.ToInt32(lie[index++]);
            ms.PlateSign = Convert.ToString(lie[index++]);
            ms.FireStormRatio = Convert.ToInt32(lie[index++]);
            ms.IceStormRatio = Convert.ToInt32(lie[index++]);
            ms.InCopperModeRatio = Convert.ToInt32(lie[index++]);
            ms.InChainModeRatio = Convert.ToInt32(lie[index++]);




            mMonsterDataDic.Add(ms.Id, ms);
        }
        #endregion

        index = 0;
        #region 分析游戏表
        text = gameCSVS;
        lineAry = text.Split('\n');
        for (int i = 0; i < lineAry.Length; i++)
        {
            if (lineAry[i].StartsWith("#"))
                continue;

            if (string.IsNullOrEmpty(lineAry[i]))
                continue;

            lineAry[i] = lineAry[i].Replace("\r", "");
            lineAry[i] = lineAry[i].TrimEnd(',');

            string[] lie = lineAry[i].Split(',');
            SetGameInitVal(lie[1], lie[2]);
        }
        #endregion



        SetSystemParameter();
    }

    public void SetSystemParameter()
    {

    }

    public void SetGameInitVal(string str, string val)
    {

    }

    public string ConvertMonsterPath(int id)
    {

        return string.Empty;
    }

    public MetaData.MonsterData GetMonster(int id)
    {
        if (mMonsterDataDic.ContainsKey(id))
            return mMonsterDataDic[id];
        else
        {
            PLDebug.LogError("not find metaMonster Id:" + id);
            return null;
        }
    }

    public MetaData.MonsterData GetMonsterByType(int type)
    {
        foreach (var item in mMonsterDataDic.Values)
        {
            if (item.Type == type)
                return item;
        }

        return null;
    }

    public Dictionary<int, MetaData.MonsterData> GetAllMonster()
    {
        return mMonsterDataDic;
    }

    public List<MetaData.ChainMonsterData> GetAllChainMonster()
    {
        return mChinaDataList;
    }



}

namespace MetaData
{
    [Serializable]
    public class MonsterData
    {
        public int Id { get; set; }
        public int Type { get; set; }
        public int HP { get; set; }
        public float Speed { get; set; }
        public int Harm { get; set; }
        public int Difficulty { get; set; }
        public int CopperRatio { get; set; }
        public int TicketRatio { get; set; }
        public int RuneStoneRatio { get; set; }
        public int GoldRatio { get; set; }

        public int InCopperModeRatio { get; set; }
        public int InChainModeRatio { get; set; }


        public int FireStormRatio { get; set; }
        public int IceStormRatio { get; set; }

        public int CopperNum { get; set; }
        public int TicketNum { get; set; }
        public int RuneStoneNum { get; set; }
        public string AssetPath { get; set; }
        public string ScreenEffect { get; set; }
        public string AudioName { get; set; }
        public int Energy { get; set; }

        /// <summary>
        /// 怪物死亡是否有提示
        /// </summary>
        public string PlateSign { get; set; }
    }



    [Serializable]
    public class MonsterRoot
    {
        public MonsterData[] Info { get; set; }
    }

    [Serializable]
    public class ChainMonsterData
    {
        public int Id { get; set; }
        public string Icon { get; set; }
        public int HP { get; set; }
        public float Speed { get; set; }
        public float Score { get; set; }
        public string ChinaIcon { get; set; }
        public int CopperRatio { get; set; }
        public int CopperNum { get; set; }
    }


    [Serializable]
    public class GuideData
    {
        public int Id { get; set; }
        public int Name { get; set; }
        public int Cond { get; set; }
        public int[] Args { get; set; }
        public int Pass { get; set; }
    }

    [Serializable]
    public class GuideSetpData
    {
        public int StepId { get; set; }

        public int GuidId { get; set; }

        public int DelayTime { get; set; }


        //寻找信息

        public string UIEnum { get; set; }
        public string NodeName { get; set; }
        public string SpecialArg { get; set; }
        public string Special { get; set; }

        //界面信息
        public string UIText { get; set; }

        public string Sound { get; set; }

        public EGuideStyle Style { get; set; }

        public int Width { get; set; }

        public int Height { get; set; }

        public int Circle { get; set; }
    }

    public enum EGuideStyle
    {
        Cricle,
        Rect
    }

}

